
package com.scs.application.modules.settlement.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.exceptions.BusinessException;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.utils.BeanValidators;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.design.service.QueryGenerator;
import com.scs.application.modules.settlement.consts.InvoiceGroupType;
import com.scs.application.modules.settlement.entity.BillItem;
import com.scs.application.modules.settlement.mapper.BillItemMapper;
import com.scs.application.modules.settlement.request.BillItemQueryRequest;
import com.scs.application.modules.settlement.service.BillItemService;
import com.scs.application.modules.settlement.vo.BillItemVO;
import lombok.AllArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 记账单明细 controller
 */
@AllArgsConstructor
@RestController
@RequestMapping("/settlement/bill_items")
public class BillItemController extends CrudController<BillItem, String, BillItemService> implements ImAndExAbleController<BillItemQueryRequest> {
    private final BillItemMapper billItemMapper;

    @PostMapping
    public CommonResult add(@RequestBody @Valid BillItem entity) {
        service.add(entity);
        return CommonResult.ok("新增成功", entity);
    }

    @Override
    @PutMapping
    public CommonResult update(@RequestBody @Valid BillItem entity) {
        service.add(entity);
        return CommonResult.ok("保存成功", entity);
    }

    @GetMapping("/page")
    public PageResult page(BillItemQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/page_for_invoice")
    public PageResult pageForInvoice(BillItemQueryRequest request) {
        IPage page = service.pageForInvoice(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }


    @GetMapping("/list")
    public CommonResult list(BillItemQueryRequest request) {
        List<BillItem> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }


    @Override
    @DeleteMapping("/{id}")
    public CommonResult deleteById(@PathVariable("id") String id) {
        service.delete(id);
        return CommonResult.ok("删除成功");
    }

    @PutMapping("/batch_save")
    public CommonResult batchSave(@RequestBody @Valid List<BillItem> children) {
        Assert.notEmpty(children, "至少有一条");
        BeanValidators.validateListWithException(validator, children);
        service.batchSave(children);
        return CommonResult.ok("保存成功");
    }

    /***
     * @param request
     * @Description:  开票新增查询
     */
    @GetMapping("/pageForInvoiceAdd")
    public PageResult pageForInvoiceAdd(HttpServletRequest request,@RequestParam Map<String, String> reqData) {
        String groupType="",hospitalId="";
        if (reqData.containsKey("userParam")) {
            JSONObject ob = JSON.parseObject(reqData.get("userParam") + "");
            groupType = ob.getString("groupType");
            hospitalId = ob.getString("hospitalId");
        }
        BusinessException.throwErrorIf(StringUtils.isBlank(groupType),"查询失败，分组条件缺失");

        IPage<BillItemVO> page = QueryGenerator.parsePage(request);
        QueryWrapper<BillItemVO> queryWrapper = QueryGenerator.initQueryWrapper(request);
        IPage pageResult = billItemMapper.pageForInvoiceAdd(page, queryWrapper,hospitalId,InvoiceGroupType.getByGroupType(groupType),InvoiceGroupType.getByGroupType(groupType));
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }
}
